package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.telecom.Call;
import android.util.Log;

import com.huawei.hms.network.base.common.MediaType;
import com.huawei.hms.network.httpclient.Callback;


import com.huawei.hms.network.httpclient.Response;
import com.huawei.hms.site.api.SearchResultListener;
import com.huawei.hms.site.api.SearchService;
import com.huawei.hms.site.api.SearchServiceFactory;
import com.huawei.hms.site.api.model.Coordinate;
import com.huawei.hms.site.api.model.DetailSearchRequest;
import com.huawei.hms.site.api.model.DetailSearchResponse;
import com.huawei.hms.site.api.model.HwLocationType;
import com.huawei.hms.site.api.model.LocationType;
import com.huawei.hms.site.api.model.NearbySearchRequest;
import com.huawei.hms.site.api.model.NearbySearchResponse;
import com.huawei.hms.site.api.model.SearchStatus;
import com.huawei.hms.site.api.model.Site;
import com.huawei.hmsdemo10086.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class StoreActivity extends AppCompatActivity {

    private SearchService searchService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);

        initview();
    }


    private void initview() {
        // Declare a SearchService object.

        // Instantiate the SearchService object.
        searchService = SearchServiceFactory.create(this, "CgB6e3x9hJVU1Ev5onqhI2ust4TQicMbUi9dp7VuF2kyc4w1Jp6/d/jzByV5rKOJHpTlposBjkRd/RDI2LSOJg8z");
        // Create a request body.
        NearbySearchRequest request = new NearbySearchRequest();
        Coordinate location = new Coordinate(102.28930939648436, 36.20488591632123);
        request.setLocation(location);
        //request.setQuery("");
        request.setRadius(1000);
        request.setHwPoiType(HwLocationType.ADDRESS);
        //request.setLanguage("fr");
        request.setPageIndex(2);
        request.setPageSize(5);
        request.setStrictBounds(false);
        // Create a search result listener.
        SearchResultListener<NearbySearchResponse> resultListener = new SearchResultListener<NearbySearchResponse>() {
            // Return search results upon a successful search.
            @Override
            public void onSearchResult(NearbySearchResponse results) {
                if (results == null || results.getTotalCount() <= 0) {
                    return;
                }
                List<Site> sites = results.getSites();
                if (sites == null || sites.size() == 0) {
                    return;
                }
                for (Site site : sites) {
                    System.out.println("siteId:" + site.getSiteId() + "name:" + site.getName());

                }
            }

            // Return the result code and description upon a search exception.
            @Override
            public void onSearchError(SearchStatus status) {
                System.out.println("Error");
                Log.i("TAG", "Error : " + status.getErrorCode() + " " + status.getErrorMessage());
            }
        };
        // Call the nearby place search API.
        searchService.nearbySearch(request, resultListener);

    }

    public void getdetail() {

        // Declare a SearchService object.

// Instantiate the SearchService object.
        searchService = SearchServiceFactory.create(this, "CgB6e3x9hJVU1Ev5onqhI2ust4TQicMbUi9dp7VuF2kyc4w1Jp6/d/jzByV5rKOJHpTlposBjkRd/RDI2LSOJg8z");
// Create a request body.
        DetailSearchRequest request = new DetailSearchRequest();
        request.setSiteId("C2B922CC4651907A1C463127836D3957");
        request.setLanguage("fr");
        request.setChildren(false);
// Create a search result listener.
        SearchResultListener<DetailSearchResponse> resultListener = new SearchResultListener<DetailSearchResponse>() {
            // Return search results upon a successful search.
            @Override
            public void onSearchResult(DetailSearchResponse result) {
                Site site;
                if (result == null || (site = result.getSite()) == null) {
                    return;
                }
                Log.i("TAG", String.format("siteId: '%s', name: %s\r\n", site.getSiteId(), site.getName()));
            }

            // Return the result code and description upon a search exception.
            @Override
            public void onSearchError(SearchStatus status) {
                Log.i("TAG", "Error : " + status.getErrorCode() + " " + status.getErrorMessage());
            }
        };
// Call the place detail search API.
        searchService.detailSearch(request, resultListener);

    }


}
